import { Body, Controller, Param, Post, Query, Res } from '@nestjs/common';
import { WebsiteService } from './website.service';
import { Get } from '@nestjs/common';
import { SkipAuth } from 'src/decorators/skipAuth';
import { CreateCommentDto } from './dto/create.dto';

@SkipAuth()
@Controller()
export class WebsiteController {
  constructor(private readonly webSiteService: WebsiteService) {}

  /**
   * 查询所有标签
   *
   * @returns {{ list:Tag[], total:number }} 标签列表和总数
   */
  @Get('tags')
  async getAllTags() {
    const { tags, total } = await this.webSiteService.getAllTags();
    return {
      list: tags,
      total,
    };
  }

  /**
   * 根据 文章类型查询文章或随记
   *
   * @param { number } page - 页码
   * @param { number } pageSize - 页面大小
   * @param { number } type - 文章类型
   * @returns {{ list:Article[], total:number }} 文章列表及总数
   */
  @Get('articles')
  async getArticles(@Query() query) {
    const { page, pageSize, type, priorCreate } = query;
    const [articles, total] = await this.webSiteService.getArticles(
      +page,
      +pageSize,
      +type,
      priorCreate,
    );
    return {
      list: articles,
      total,
    };
  }

  /**
   * 根据 文章ID查询文章内容
   *
   * @param { number } articleId - 文章ID
   * @returns {{ article:Article }} 文章列表及总数
   */
  @Get('articleInfo/:articleId')
  async getArticlInfo(@Param() param) {
    const { articleId } = param;
    return this.webSiteService.getArticlInfo(+articleId);
  }

  /**
   * 发表评论
   *
   * @param { number } articleId - 评论文章ID
   * @param { string } commentNickname - 评论者用户名
   * @param { commentAvatar } 评论人头像
   * @param { string } comment - 评论内容
   * @param { string } commentEmail - 评论者邮箱
   * @param { string } website - 评论者网站
   * @param { parentId } 被回复评论ID
   * @param { repliedNickname } 被回复人昵称
   * @returns { MsgResponse } 是否成功
   */
  @Post('comment')
  async publishComment(@Body() newComment: CreateCommentDto, @Res() res) {
    await this.webSiteService.publishComment(newComment);
    res.send({
      code: 0,
      msg: '评论成功',
    });
  }

  /**
   * 根据文章ID查询对应评论列表
   *
   * @param { articleId } 文章ID
   * @returns {{ list: Comment[] }} 评论列表 数量
   */
  @Get('comments')
  async getCommentTree(@Query('articleId') articleId) {
    const commentTree = await this.webSiteService.getCommentTree(+articleId);
    return {
      list: commentTree,
      total: commentTree.length,
    };
  }
}
